
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/request/request.dart';
import 'package:local_coffee/utils/loading.dart';
import 'package:local_coffee/utils/pull_to_refresh_style.dart';
import 'package:local_coffee/utils/user.dart';

class G {
  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  static final List toolbarRouteNameList = [
    '/',
    '/menu',
    '/order',
    '/shoping_card',
    'mine'
  ];

  /// 处理商品描述
  static String handleGoodsDesc(String str) {
    return str
      .replaceAll(RegExp(',\$'), '')
      .replaceAll(RegExp('规格:|温度:|糖度:|奶油:|无'), '');
  }

  static final Request req = Request();

  static Future toast(String text) => Fluttertoast.showToast(
    msg: text,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIos: 1,
    backgroundColor: Colors.red,
    textColor: Colors.white,
    fontSize: 16.0
  );

  /// 初始化 loading
  static final Loading loading = Loading();

  /// 手动延时
  static sleep({
    int milliseconds = 1000
  }) async => await Future.delayed(Duration(milliseconds: milliseconds));

  /// 下拉刷新样式
  static final PullToRefreshStyle pullToRefreshStyle = PullToRefreshStyle();

  /// 获取当前 state
  static NavigatorState getCurrentState() => navigatorKey.currentState;

  /// 获取当前 context
  static BuildContext getCurrentContext() => navigatorKey.currentContext;

  /// 获取屏幕上下边距
  /// 用于兼容全面屏, 刘海屏
  static EdgeInsets screenPadding() =>
    MediaQuery.of(getCurrentContext()).padding;

  /// 获取屏幕宽度
  static double screenWidth() => MediaQuery.of(getCurrentContext()).size.width;

  /// 获取屏幕高度
  static double srceenHeight() => MediaQuery.of(getCurrentContext()).size.height;

  /// 跳转页面. 使用 G.pushNamed
  static void pushNamed(String routeName, {Object arguments}) {
    // 如果跳转到 toolbar 页面, 不能返回
    if (toolbarRouteNameList.indexOf(routeName) > -1) {
      getCurrentState().pushReplacementNamed(
        routeName,
        arguments: arguments
      );
    } else {
      getCurrentState().pushNamed(
        routeName,
        arguments: arguments
      );
    }
  }

  /// 返回页面
  static void pop() => getCurrentState().pop();

  /// 底部 border
  /// ```
  /// @param {Color} color
  /// @param {bool} show 是否显示底部 border
  /// ```
  static Border borderBottom({
    Color color,
    bool show = true
  }) {
    return Border(
      bottom: BorderSide(
        color: (color == null || show) 
          ? (show ? RGBA.rgbaf2f2f2 : Colors.transparent)
          : color,
        width: 1
      )
    );
  }

  /// 获取时间戳
  /// ```
  /// @param {DateTime} time time = null 获取当前时间戳
  /// ```
  static int getTime([
    DateTime time
  ]) {
    if (time == null) {
      return (DateTime.now().millisecondsSinceEpoch / 1000).round();
    } else {
      return (time.millisecondsSinceEpoch / 1000).round();
    }
  }

  /// user 信息
  static final User user = User();
}